package cn.com.controller;

import cn.com.pojo.CommentBean;
import cn.com.pojo.FoodBean;
import cn.com.pojo.FoodListBean;
import cn.com.service.FoodBeanService;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/Menu")
public class FoodBeanController {
    @Resource
    private FoodBeanService foodBeanService;
    @RequestMapping(value = "/count",method = RequestMethod.POST)
    @ResponseBody
    public Object countAll(){
        int count = foodBeanService.countAll();
        return JSONArray.toJSONString(count);
    }
    @RequestMapping(value = "/all",method = RequestMethod.POST)
    @ResponseBody
    public Object findAll(){
        List<FoodBean> foodBeans = foodBeanService.findAll();
        FoodListBean listBean = new FoodListBean();
        listBean.setMsg("成功");
        listBean.setSuccess(true);
        listBean.setList(foodBeans);


        return JSONArray.toJSONString(listBean);
    }
    @RequestMapping(value = "/byId",method = RequestMethod.POST)
    @ResponseBody
    public Object findNameById(@RequestParam("id") Integer id){
        FoodBean foodBean = foodBeanService.findAllById(id);
        return JSONArray.toJSONString(foodBean);
    }
    @RequestMapping(value = "/foodPrice",method = RequestMethod.POST)
    @ResponseBody
    public Object findPriceById(@RequestParam("id") Integer id){
        FoodBean foodBean = foodBeanService.findPriceById(id);
        return JSONArray.toJSONString(foodBean);
    }
    @Resource
    private FoodBeanService commentBeanService;
    @RequestMapping(value = "/foodComment",method = RequestMethod.POST)
    @ResponseBody
    public Object findCommentById(@RequestParam("id") Integer id){
        CommentBean commentBean = commentBeanService.findCommentById(id);
        return JSONArray.toJSONString(commentBean);
    }
    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    @ResponseBody
    public Object add(CommentBean commentBean){
        String msg = "";
        if (commentBeanService.add(commentBean)>0){
            msg = "成功";
        }else {
            msg = "失败";
        }
        return JSONArray.toJSONString(msg);
    }

}
